package com.ck.ck_back.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ck.ck_back.pojo.Result;
import com.ck.ck_back.pojo.User;
import com.ck.ck_back.service.userservice;
import com.ck.ck_back.utils.md5_add;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private userservice userserviceImpl;

    //    返回参数，0是用户已在表中的，1是插入成功的，403是参数错误，404参数为空
    @PostMapping("/adduser")
    public Object test(User user){
        int result = userserviceImpl.adduser(user);
        User user1=userserviceImpl.selectbyopenid(user.getOpenid());
        if (result == 0){
            Map map = new HashMap();
            map.put("openid",user1.getOpenid());
            map.put("name",user1.getName());
            map.put("introduce",user1.getIntroduce());
            map.put("avatarurl",user1.getAvatarurl());
            map.put("identity",user1.getIdentity()?1:0);
           return Result.success(map);
        }else if (result == 1){
            Map map = new HashMap();
            map.put("title","欢迎新用户");
            map.put("sequence",userserviceImpl.selectalluser());
            map.put("openid",user1.getOpenid());
            map.put("name",user1.getName());
            map.put("avatarurl",user1.getAvatarurl());
            map.put("identity",user1.getIdentity()?1:0);
            return Result.success(map);
        }else if(result == 403){
            return Result.failure("传入的参数出现关键错误,id,name,头像，身份不能为空，请仔细检查");
        }else {
            return Result.failure("查无此人");
        }
    }

    //      查询用户的身份，0/1表身份，2查无此人，404参数错误
    @GetMapping("/getidentity")
    public Object get_identity(String openid){
        int result = userserviceImpl.identity(openid);
        if (result == 0 || result == 1){
            return Result.success(result);
        }else if(result == 2){
            return Result.failure("查无此人");
        }else {
            return Result.failure("关键参数为空，请仔细检查");
        }
    }

    @PostMapping("/getuser")
    public Object getuser(String openid){
        User user = userserviceImpl.selectbyopenid(openid);
        if (user !=null){
            return Result.success(user);
        }else {return Result.failure("失败了");}

    }

//      改变身份，0/1成功，为身份值，2查无此人，404参数错误
    @PostMapping("/changeidentity")
    public  Object changeidentity(String openid){
        int result = userserviceImpl.changeidentity(openid);
        if (result == 0 || result == 1){
            return Result.success(result);
        }else if(result == 2){
            return Result.failure("查无此人");
        }else {
            return Result.failure("关键参数为空，请仔细检查");
        }
    }

//      重置作者端密码 0重置成功，1身份错误，2查无此人，404参数错误
    @PostMapping("/resetauthor")
    public Object resetauthor(String openid){
        int result = userserviceImpl.resetauthor(openid);
        System.out.println(result);
        if(result == 0){
            User user=userserviceImpl.selectbyopenid(openid);
            Map map = new HashMap<>();
            map.put("AuthorUserName",user.getAuthorUsername());
            map.put("AuthorPwd",md5_add.md5MinusSalt(user.getAuthorPwd()));
            return Result.success(map);
        }else if(result == 1){
           return Result.failure("此人不是作者");
        }else if(result == 2){
            return Result.failure("查无此人");
        }else {
            return Result.failure("关键参数为空，请仔细检查");
        }

    }

    //    查询作者个人详细信息 0正常，1身份错误，2查无此人，404参数错误
    @PostMapping("/selectauthor")
    public Object selectauthor(String openid){
        int result = userserviceImpl.selectauthor(openid);
        if (result == 0){
            User user = userserviceImpl.selectbyopenid(openid);
            user.setAuthorPwd(md5_add.md5MinusSalt(user.getAuthorPwd()));
            return Result.success(user);
        }else if (result == 1){
            return Result.failure("身份错误");
        }else if (result == 2){
            return Result.failure("查无此人");
        }else {
            return Result.failure("参数错误");
        }
    }

    //    更改作者端的密码,1为正确，2表示身份错误，404参数错误
    @PostMapping("/updateauthor")
    public Object updateAuthor(String openid,String pwd){
        int result = userserviceImpl.updateAuthor(openid,pwd);
        if (result == 1){
            return Result.success("更新成功");
        }else if(result == 2){
            return Result.failure("身份错误");
        }else  {
            return Result.failure("关键参数为空，请仔细检查");
        }
    }

    //    实现登陆，0为登陆成功,1为密码错误，2为身份错误，3无此人，404参数错误
    @PostMapping("/login")
    public Object login(String openid,String pwd){
        int result =userserviceImpl.login(openid,pwd);
        if (result == 0){
            User user=userserviceImpl.selectbyopenid(openid);
            String token = StpUtil.getTokenInfo().tokenValue;
            Map map =new HashMap();
            map.put("name",user.getName());
            map.put("avatarurl",user.getAvatarurl());
            map.put("introduce",user.getIntroduce());
//            map.put("token",token);

            return Result.success(map);
        }else if (result == 1){
            return Result.failure("密码错误");
        }else if (result == 2){
            return Result.failure("身份错误");
        }else if (result == 3){
            return Result.failure("查无此人");
        }else{
        return Result.failure("关键参数为空，请仔细检查");
        }
    }
    //    上传文件,404参数错误
    @RequestMapping("/upload")
    public Object upload(MultipartFile file){
//        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
//        MultipartFile file = req.getFile("file");
        if (file.isEmpty()){
            System.out.println("kong");
        }
//        MultipartFile file = files[0];
        String result = userserviceImpl.upload(file);
        if (!result.equals("404")){
            return Result.success(result);
        }else {
            return Result.failure("关键参数为空，请仔细检查");
        }
    }

    //    更新用户信息 , 404参数错误 ， 1正常 ，2更新错误 ，3 查无此人
    @PostMapping("/update")
    public Object update(User user){
        int result = userserviceImpl.updateUser(user);
        if (result == 1){
//            获取全新的值，返回
            User user1 = userserviceImpl.selectbyopenid(user.getOpenid());
            Map map = new HashMap();

            map.put("openid",user1.getOpenid());
            map.put("name",user1.getName());
            map.put("introduce",user1.getIntroduce());
            map.put("avatarurl",user1.getAvatarurl());
            map.put("identity",user1.getIdentity()?1:0);

            return Result.success(map);
        }else  if ( result == 2){
            return Result.failure("更新失败");
        }else  if ( result == 3){
            return Result.failure("查无此人");
        }else {
            return Result.failure("关键参数为空，请仔细检查");
        }
    }

    @PostMapping("/getproduction")
    public Object getproduction(String openid,int state){
        return userserviceImpl.select_author_production(openid,state);
    }


    @PostMapping("/getaudit")
    public Object getaudit(){
        return Result.success(userserviceImpl.getaudit());
    }

    @PostMapping("/upstate")
    public Object upstate(String openid,int state){
        int result = userserviceImpl.updatestate(openid, state);
        if (result == 1){
            return Result.success();
        }else if (result == 2){
            return Result.failure("更新失败");
        }else if (result == 3){
            return Result.failure("无此人");
        }else {
            return Result.failure("参数错误");
        }
    }
}
